package com.stars.module.setting.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.stars.base.constants.CommonNumberConstants;
import com.stars.base.constants.RedisConstants;
import com.stars.base.constants.SeparatorConstants;
import com.stars.base.constants.SystemSettingConstants;
import com.stars.base.entity.SessionUser;
import com.stars.module.code.service.CodeService;
import com.stars.module.setting.entity.SystemSetting;
import com.stars.module.setting.entity.SystemSettingVo;
import com.stars.module.setting.mapper.SystemSettingMapper;
import com.stars.module.setting.service.SystemSettingService;
import com.stars.util.cache.CacheUtil;
import com.stars.util.common.DevelopKit;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SystemSettingServiceImpl implements SystemSettingService {

    private final SystemSettingMapper settingMapper;
    private final CacheUtil cacheUtil;
    private final CodeService codeService;

    public SystemSettingServiceImpl(SystemSettingMapper settingMapper, CacheUtil cacheUtil, CodeService codeService) {
        this.settingMapper = settingMapper;
        this.cacheUtil = cacheUtil;
        this.codeService = codeService;
    }


    @Override
    public List<SystemSettingVo> loadAllDefine(String type) {

        SessionUser user = DevelopKit.getSessionUser();
        assert user != null;

        List<SystemSetting> defineList = settingMapper.loadAllDefine(type);
        List<SystemSetting> settingList;

        if (CommonNumberConstants.ONE_STR.equals(type)) {
            // 查询系统设置
            settingList = settingMapper.loadAllSettings();
        } else if (CommonNumberConstants.TWO_STR.equals(type)) {
            // 查询机构设置
            settingList = settingMapper.loadAllUnitSettings(user.getCurrentUnit());
        } else {
            // 查询个人设置
            settingList = settingMapper.loadAllUserSettings(user.getUserId());
        }

        Map<String, SystemSetting> settingMap = settingList.stream().collect(Collectors.toMap(SystemSetting::getFid, o -> o));

        return defineList.stream().map(define -> {
            SystemSettingVo vo = new SystemSettingVo();
            vo.setConfigValueList(Collections.emptyList());
            BeanUtils.copyProperties(define,vo);
            if (SystemSettingConstants.EL_CHECKBOX.equals(define.getUseControl()) && StringUtils.hasLength(define.getConfigValue())) {
                vo.setConfigValueList(Arrays.asList(define.getConfigValue().split(SeparatorConstants.COMMA_SEPARATOR)));
            }
            if (StringUtils.hasLength(define.getAction())) {
                JSONArray codeArr = codeService.getCode(define.getAction());
                vo.setDataSource(codeArr);
            }
            if (settingMap.containsKey(define.getId())) {
                SystemSetting setting = settingMap.get(define.getId());
                vo.setConfigValue(setting.getConfigValue());
                vo.setId(setting.getId());
                vo.setFid(define.getId());
            } else {
                vo.setId(DevelopKit.generateUUID());
                vo.setFid(define.getId());
            }
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public boolean saveAllDefine(List<SystemSettingVo> list, String type) {

        SessionUser user = DevelopKit.getSessionUser();
        assert user != null;

        if (CommonNumberConstants.ONE_STR.equals(type)) {
            settingMapper.deleteAllSetting();
        } else if (CommonNumberConstants.TWO_STR.equals(type)) {
            settingMapper.deleteAllUnitSetting(user.getCurrentUnit());
        } else {
            settingMapper.deleteAllUserSetting(user.getUserId());
        }

        for ( SystemSettingVo entity : list ) {
            SystemSetting setting = new SystemSetting();
            setting.setId(DevelopKit.generateUUID());
            BeanUtils.copyProperties(entity, setting);
            if (SystemSettingConstants.EL_CHECKBOX.equals(entity.getUseControl())) {
                setting.setConfigValue(String.join(SeparatorConstants.COMMA_SEPARATOR,entity.getConfigValueList()));
            }
            DevelopKit.setBaseInfo(setting);
            if(settingMapper.insert(setting) < 1) {
                return false;
            }
        }
        if (CommonNumberConstants.ONE_STR.equals(type)) {
            cacheUtil.clearCache(false, RedisConstants.SETTING_PREFIX);
        } else if (CommonNumberConstants.TWO_STR.equals(type)) {
            cacheUtil.clearCache(false, RedisConstants.UNIT_SETTING_PREFIX + user.getCurrentUnit());
        } else {
            cacheUtil.clearCache(false, RedisConstants.USER_SETTING_PREFIX + user.getUserId());
        }

        return true;
    }
}
